package com.aaa.car.controller;

import com.aaa.car.entity.Shop;
import com.aaa.car.service.ShopService;
import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.Role;

/**
 * (Shop)表控制层
 *
 * @author makejava
 * @since 2020-11-18 08:53:41
 */
@RestController
@RequestMapping("shop")
public class ShopController {
    /**
     * 服务对象
     */
    @Resource
    private ShopService shopService;

    /**
     * 分页加查询所有门店
     * @param pageNo
     * @param pageSize
     * @param shop
     * @return
     */
    @GetMapping("queryAllShop")
    public Result queryAllShop(Integer pageNo, Integer pageSize, Shop shop){
        return shopService.queryAllShop(pageNo,pageSize,shop);
    }
    /**
     * 添加系统角色
     * @param shop
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody Shop shop){
        //判断新增的角色名字为空抛出异常
        if (shop.getName()==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }else{
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.shopService.insert(shop));
        }
    }

    /**
     * 根据角色id修改角色信息
     * @param shop
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody  Shop shop){
        //当ID空为时抛出自定义异常
        if (shop.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.shopService.update(shop));
    }

    /**
     * 根据角色id删除角色信息
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id){
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.shopService.deleteById(id));
    }

}